/*
 PureMVC AS3 Demo - Flex Application Skeleton 
 Copyright (c) 2007 Daniele Ugoletti <daniele.ugoletti@puremvc.org>
 Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
package com.nstar.base.util
{
	import com.nstar.base.util.Logger;
	
	import mx.utils.ObjectUtil;
	/*
	var myXML:XML =
	<books>
		<book name="flex tutorial">
			<price>30</price>
			<author>adobe</author>
		</book>
		<book name="sliverlight tutorial">
			<price>40</price>
			<author>microsoft</author>
		</book>
		<book name="java tutorial">
			<price>50</price>
			<author>sun</author>
		</book>
		<author>cyeric</author>
	</books>
	<books>
		<book name="flex tutorial">
			<price>30</price>
			<author>adobe</author>
		</book>
		<book name="sliverlight tutorial">
			<price>40</price>
			<author>microsoft</author>
		</book>
		<book name="java tutorial">
			<price>50</price>
			<author>sun</author>
		</book>
		<author>cyeric</author>
	</books>

	
	trace(myXML.book[0]) ; 
	trace(myXML..author); //使用".."操作符访问所有以该标签命名的节点，而忽略节点的上下关系
	trace(myXML.book[0].@name) //使用"@"操作符访问节点属性
	//"."，"[]"，".."，"@",最常用的4个E4X操作符
	trace(myXML.book.(price<50)); //E4X的过滤功能
	trace(myXML.book.(@name=='flex tutorial')); //属性也可以过滤
	
	trace(myXML.book.(price=5)); //把查询到的XML文本给改了
	//修改
	myXML.insertChildAfter(myXML.book[0],<newbook name="After"/>);   
	myXML.insertChildBefore(myXML.book[0],<newbook name="Before"/>);   
	myXML.appendChild(<newbook name="Append"/>);   
	myXML.prependChild(<newbook name="Prepend"/>);  	
	
	myXML.book[0].@date="2008";//添加属性
	
	myXML.book[0].author="奥多比"; //修改XML对象
	
	//删除节点
	delete myXML.book[0].@name;     
	delete myXML.book[0].author;     
	delete myXML.book[0].price.text()[0]; 	

	 */ 
	public class XmlResource
	{
		static public function parseXML(data:Object, node:Object, prefix:String=''):void
		{
			for(var i:Number=0;i<node.childNodes.length;i++)
			{
				var currentNode:Object = node.childNodes[i];
				if (currentNode.nodeName=='param' || currentNode.nodeName=='item')
				{
					if (currentNode.attributes.value!=null)
						data[(prefix+currentNode.attributes.name).toLowerCase()] = currentNode.attributes.value;
					else
						data[(prefix+currentNode.attributes.name).toLowerCase()] = currentNode.firstChild.nodeValue;		
				}
				else if (currentNode.nodeName=='group' && currentNode.hasChildNodes())
				{
					XmlResource.parseXML(data, currentNode, prefix+currentNode.attributes.name+'/');	
					continue;
				}
				if (currentNode.hasChildNodes()) XmlResource.parseXML(data, currentNode, prefix);
			}
		}
		
		//前缀prefix以 . 号分割
		static public function parseObject(data:Object, node:Object, prefix:String = ""):void{
			var ary:Array = prefix.split(".");
			var temp:Object = ObjectUtil.copy(node);
			for(var i:int =0;i<ary.length;i++){
				temp = temp[ary[i]];
			}
			
			data.result = temp;
		}
		
		
	}
}
